//
//  RegisterViewController.m
//  Commerce
//
//  Created by xiaofeifei on 15/2/3.
//  Copyright (c) 2015年 fei. All rights reserved.
//

#import "RegisterViewController.h"
#define count 60

@interface RegisterViewController (){
    NSString *_code;
    BOOL _isAgree;
    NSTimer *_timer;
    int _count;
}

@property (weak, nonatomic) IBOutlet UITextField *accountTextfield;
@property (weak, nonatomic) IBOutlet UITextField *codeTextfield;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextfield;
@property (weak, nonatomic) IBOutlet UISegmentedControl *seg;
@property (weak, nonatomic) IBOutlet UIButton *agreeBtn;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isAgree = YES;
    [_agreeBtn setImage:[UIImage imageNamed:@"select_s.png"]forState:UIControlStateNormal];
    _count = count;
    // Do any additional setup after loading the view from its nib.
}

#pragma mark -IBAction
- (IBAction)clickIsAgreeProtocol:(id)sender{
    _isAgree = !_isAgree;
    if (_isAgree) {
        [_agreeBtn setImage:[UIImage imageNamed:@"select_s.png"]forState:UIControlStateNormal];
    }else{
        [_agreeBtn setImage:[UIImage imageNamed:@"select_n.png"]forState:UIControlStateNormal];
    }
}

- (IBAction)getIdentifyingCode:(id)sender{//获取验证码
    if (_accountTextfield.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入手机号码"];
        return;
    }
    [self getUserExsits];
}

- (IBAction)gotoRegister:(id)sender{
    if (_accountTextfield.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入手机号码"];
        return;
    }
    if (_codeTextfield.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入验证码"];
        return;
    }
    if (_pwdTextfield.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入密码"];
        return;
    }
    if (_pwdTextfield.text.length < 6) {
        [SVProgressHUD showInfoWithStatus:@"密码长度不得少于6位"];
        return;
    }
    if (!_isAgree) {
        [SVProgressHUD showInfoWithStatus:@"请阅读用户协议并勾选"];
        return;
    }
    [self getRegister];
}

#pragma mark -请求接口
//判断用户是否存在
- (void)getUserExsits{
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    //传入的参数
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    [parameters addEntriesFromDictionary:@{@"user_name":_accountTextfield.text}];
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@user/exsits",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  if ([result[@"result"] intValue]==0) {
                                      [self getCode];
                                  }else{
                                      [SVProgressHUD showInfoWithStatus:@"此号码已注册"];
                                      //            [SVProgressHUD dismiss];
                                  }
                              }];

}

//获取验证码
- (void)getCode{
    //传入的参数
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    [parameters addEntriesFromDictionary:@{@"user_name":_accountTextfield.text}];
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@user/sms",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  [SVProgressHUD dismiss];

                                  if ([result[@"result"] intValue] == 0) {
                                      [SVProgressHUD showInfoWithStatus:@"获取验证码失败"];
                                  }else if ([result[@"result"] intValue] > 0){
                                      _code = result[@"result"];
                                      _timer = nil;
                                      _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(getCountdown) userInfo:nil repeats:YES];
                                      [_codeBtn setTitle:[NSString stringWithFormat:@"%d",_count] forState:UIControlStateNormal];
                                      [_codeBtn setEnabled:NO];
                                  }
                              }];

}

//注册
- (void)getRegister{
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    //传入的参数
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    [parameters addEntriesFromDictionary:@{@"user_name":_accountTextfield.text,@"password":_pwdTextfield.text,@"user_type":[NSString stringWithFormat:@"%ld",(long)_seg.selectedSegmentIndex]}];
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@user/register",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  [SVProgressHUD dismiss];
                                  if ([result[@"UserID"] intValue] == 0) {
                                      [SVProgressHUD showInfoWithStatus:@"注册失败"];
                                  }else{
                                      [SVProgressHUD showSuccessWithStatus:@"注册成功"];
                                      [self backBtnAct:nil];
                                  }
                              }];
}

- (void)getCountdown{
    _count --;
    if (_count > 0) {
        [_codeBtn setEnabled:NO];
        [_codeBtn setTitle:[NSString stringWithFormat:@"%d",_count] forState:UIControlStateNormal];
    }else{
        [_timer invalidate];
        _count = count;
        [_codeBtn setEnabled:YES];
        [_codeBtn setTitle:@"验证码" forState:UIControlStateNormal];
    }
}

#pragma mark -UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
